//
//  MainTabBar.swift
//  微博项目
//
//  Created by 英 on 15/12/12.
//  Copyright © 2015年 英. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    // 重写父类构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeBtn)
    }
    
    // 在这个方法中设置所有的子视图的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        // 遍历所有子视图，修改大小
        var index: CGFloat = 0
        for subView in subviews {
            
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                subView.frame = CGRectOffset(rect, index * w, 0)
                
                index += (index == 1 ? 2 : 1)
            }
        
            
        }
        // 设置加号按钮的位置
        composeBtn.frame = CGRectOffset(rect, 2 * w, 0)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 懒加载  加号按钮
    lazy var composeBtn: UIButton = {
    
        // 自定义样式
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"),forState:.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
    // 根据背景视图，自动设置大小
        btn.sizeToFit()
        return btn
        
    }()
   
}
